/*
 * @lc app=leetcode.cn id=404 lang=cpp
 *
 * [404] 左叶子之和
 */

// @lc code=start

// struct TreeNode {
//     int val;
//     TreeNode *left;
//     TreeNode *right;
//     TreeNode() : val(0), left(nullptr), right(nullptr) {}
//     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
// };

class Solution
{
    bool isLeaf(TreeNode *root)
    {
        return (root != nullptr) &&
               (root->left == nullptr) &&
               (root->right == nullptr);
    }
    void search(TreeNode *root, int &ans)
    {
        if (root == nullptr)
            return;

        if (isLeaf(root->left))
        {
            ans += root->left->val;
        }
        else
        {
            search(root->left, ans);
        }

        search(root->right, ans);
        return;
    }

public:
    int sumOfLeftLeaves(TreeNode *root)
    {
        int ans = 0;
        search(root, ans);
        return ans;
    }
};
// @lc code=end
